package io.renren.modules.gaokaobaobei.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.gaokaobaobei.entity.UserBackDto;
import io.renren.modules.gaokaobaobei.entity.UserBackupEntity;
import io.renren.modules.gaokaobaobei.service.UserBackupService;
import io.renren.modules.school.entity.TemplateStudentEntity;
import io.renren.modules.school.entity.TreeEntity;
import io.renren.modules.school.entity.UniversityEntity;
import io.renren.modules.student.service.StudentService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 志愿设计管理
 *
 * @author zlb
 * @email 316682546@qq.com
 * @date 2018-10-13 19:18:27
 */
@RestController
@RequestMapping("gaokaobaobei/userbackup")
public class UserBackupController extends AbstractController {
    @Autowired
    private UserBackupService userBackupService;
    @Autowired
    private StudentService studentService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("gaokaobaobei:userbackup:list")
    public R list(@RequestParam Map<String, Object> params, String type) {
        PageUtils page = userBackupService.queryPage(params, type);
        return R.ok().put("page", page);
    }

    @RequestMapping("/getUserBackupByStudentId")
    public R getUserBackupByStudentId(@RequestParam Integer id) {
        UserBackupEntity student = userBackupService.getUserBackupByStudentId(id);
        return R.ok().put("student", student);
    }

    /**
     * 列表
     */
    @RequestMapping("/getSelfStudentlist")
    public R getSelfStudentlist(@RequestParam Map<String, Object> params) {
        PageUtils page = userBackupService.getSelfStudentlist(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("gaokaobaobei:userbackup:info")
    public R info(@PathVariable("id") Integer id) {
        UserBackupEntity userBackup = userBackupService.selectById(id);

        return R.ok().put("userBackup", userBackup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("gaokaobaobei:userbackup:save")
    public R save(@RequestBody UserBackupEntity userBackup, String typeID) {
        String username = ShiroUtils.getSessionAttribute("username") + "";
        Long userId = getUserId();
        userBackup.setAdmissionstype(typeID);
        userBackup.setUsername(username);
        userBackup.setUserId(userId);
        if(userBackup.getNature1() == null){
            List sUature1 = Arrays.asList(userBackup.getNature().split(","));
            userBackup.setNature1(sUature1);
        }
        String r = "";
        for (String s : userBackup.getNature1()) {
            r = r + s + ",";
        }
        userBackup.setNature(r);
        //计算需求

        userBackupService.insert(userBackup);
        userBackupService.saveTuijian(userBackup);
        return R.ok();
    }

    /**
     * @param userBackup
     * @return
     */


    @RequestMapping("/baobeiStudent")
    public R baobeiStudent(@RequestBody UserBackDto userBackup) {
        userBackup.setUsername(getUser().getUsername());
        userBackup.setUserId(getUserId());
        //  userBackup.setAdmissionstype("夏考");
        UserBackupEntity u = userBackupService.getByNameAndPhone(userBackup);
        // userBackup.get
        if (u == null) {
            userBackup.getNature1();
            userBackupService.insert(userBackup);
            UserBackupEntity ub = userBackupService.getNewUserBackup();
            List<UniversityEntity> universityEntitys = userBackup.getUniversitys();
            int i = 1;
            for (UniversityEntity universityEntity : universityEntitys) {
                if (!"".equals(universityEntity.getName())) {
                    universityEntity.setUserBackId(ub.getId());
                    universityEntity.setSort(i);
                    i++;
                    studentService.saveStudentUniversity(universityEntity);
                }
            }
            return R.ok();
        } else {
            Integer[] thisId = new Integer[1];
            thisId[0] = u.getId();
            this.delete(thisId);
            return baobeiStudent(userBackup);
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("gaokaobaobei:userbackup:update")
    public R update(@RequestBody UserBackupEntity userBackup) {
        ValidatorUtils.validateEntity(userBackup);
        userBackupService.updateAllColumnById(userBackup);//全部更新

        return R.ok();
    }

    /**
     * 点击审核 记录 数据及状态
     *
     * @param school  录取学校
     * @param zhuanye 录取专业
     * @return
     */
    @RequestMapping("/baobei")
    public R baobei(String school, String zhuanye, String number) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("school", school);
        map.put("zhuanye", zhuanye);
        map.put("number", number);
        map.put("type", "等待审核");
        //通过考号来更新状态
        boolean flag = userBackupService.updateTypeByUserName(map);
        return R.ok().put("flag", flag);
    }

    /**
     * 获得 用户的 设计志愿总数、审核数、通过及过期的数量
     *
     * @return
     */
    @RequestMapping("/getNumber")
    public R getNumber() {
        //获得用户姓名
        String username = ShiroUtils.getSessionAttribute("username") + "";
        Map<String, String> map = new HashMap<String, String>();
        map.put("username", username);
        map.put("type", null);
        //志愿总数
        int sum = userBackupService.findTypeByUserName(map);
        //等待审核
        map.put("type", UserBackupEntity.TYPE_DDSH);
        int wait = userBackupService.findTypeByUserName(map);
        //审核通过
        map.put("type", UserBackupEntity.TYPE_SHTG);
        int pass = userBackupService.findTypeByUserName(map);
        //已过期
        map.put("type", UserBackupEntity.TYPE_YGQ);
        int overdue = userBackupService.findTypeByUserName(map);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("<a >");
        stringBuffer.append(" 设计志愿总数:  ");
        stringBuffer.append(sum);
        stringBuffer.append("</a>");
        stringBuffer.append("<a >");
        stringBuffer.append(" 等待审核数:  ");
        stringBuffer.append(wait);
        stringBuffer.append("</a>");
        stringBuffer.append("<a >");
        stringBuffer.append(" 审核通过数:  ");
        stringBuffer.append(pass);
        stringBuffer.append("</a>");
        stringBuffer.append("<a >");
        stringBuffer.append(" 已过期:  ");
        stringBuffer.append(overdue);
        stringBuffer.append("</a>");
        return R.ok().put("number", stringBuffer);
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("gaokaobaobei:userbackup:delete")
    public R delete(@RequestBody Integer[] ids) {
        List thisList = Arrays.asList(ids);
        for (int i = 0; i < thisList.size(); i++) {
            Integer thisId = (Integer) thisList.get(i);
            userBackupService.deleteStudentSchoolAndProfession(thisId);
        }
        userBackupService.deleteBatchIds(thisList);
        return R.ok();
    }


    /**
     * 获得学校列表
     */
    @RequestMapping("/getTemplatelTree")
//    @RequiresPermissions("course:school:select")
    public R getTemplatelTree(String username) {
        List<TreeEntity> templatelTree = userBackupService.getTemplatelTree(username);

        return R.ok().put("templatelTree", templatelTree);
    }

    @RequestMapping("/saveTemplate")
    public R saveTemplate(@RequestBody TemplateStudentEntity templateStudentEntity) {
        templateStudentEntity.setUserId(getUserId().intValue());
        templateStudentEntity.setUsername(getUser().getUsername());
        userBackupService.saveTemplate(templateStudentEntity);
        return R.ok("", 0);


    }

    @RequestMapping("/getTemplateById")
    R getStudentById(Integer id) {
        List<UniversityEntity> schools = userBackupService.getTemplateById(id);
        return R.ok().put("schools", schools);
    }

    @RequestMapping("/getStudentDesign")
    public R getStudentDesign(@RequestParam Integer studentId) {
        List<UniversityEntity> schools = userBackupService.getStudentDesign(studentId);

        return R.ok().put("schools", schools);
    }

}
